package com.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.configure.Result;
import com.food.entity.Material;
import com.food.entity.MaterialLog;
import com.food.entity.PurchaseLog;
import com.food.mapper.MaterialLogMapper;
import com.food.mapper.MaterialMapper;
import com.food.mapper.PurchaseLogMapper;
import com.food.service.PurchaseLogService;
import com.food.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2021-04-04
 */
@Service
public class PurchaseLogServiceImpl extends ServiceImpl<PurchaseLogMapper, PurchaseLog> implements PurchaseLogService {

    @Resource
    PurchaseLogMapper purchaseLogMapper;

    @Resource
    MaterialMapper materialMapper;

    @Resource
    MaterialLogMapper materialLogMapper;
    /**
     * 添加/修改--采购日志
     */
    @Override
    public Result<PurchaseLog> updatePurchaseLog(PurchaseLog purchaseLog) {
        Result<PurchaseLog> result = new Result<>();
        try {
            if (StringUtils.isEmpty(purchaseLog.getPurchaseLogId())) {
                //如果ID为空，代表添加
                purchaseLog.setPurchaseLogId(IDGenerator.getUUID("PLOG"));
                purchaseLog.setCreateTime(new Date());
                //采购状态设置为未采购
                purchaseLog.setPurchaseStatus("0");
                purchaseLogMapper.insert(purchaseLog);
            } else {
                //如果ID不为空，代表修改
                purchaseLogMapper.updateById(purchaseLog);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<PurchaseLog> delPurchaseLog(PurchaseLog purchaseLog) {
        Result<PurchaseLog> result = new Result<>();
        try {
            if (StringUtils.isEmpty(purchaseLog.getPurchaseLogId())) {
                result.setMessage("purchaseLogId can't be empty");
                return result;
            }
            purchaseLogMapper.deleteById(purchaseLog.getPurchaseLogId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<PurchaseLog> batchDelPurchaseLog(PurchaseLog purchaseLog) {
        Result<PurchaseLog> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(purchaseLog.getIds())) {
                String[] ids = purchaseLog.getIds().split(",");
                for (String id : ids) {
                    purchaseLogMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<PurchaseLog> queryPurchaseLog(PurchaseLog purchaseLog) {
        Result<PurchaseLog> result = new Result<>();
        try {
            QueryWrapper<PurchaseLog> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用

            List<PurchaseLog> purchaseLogs = purchaseLogMapper.selectList(wrapper);
            result.setReturnList(purchaseLogs);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 采购日志分页查询
     * @param purchaseLog
     * @return
     */
    @Override
    public Result<PurchaseLog> queryPurchaseLogByPage(PurchaseLog purchaseLog) {
        Result<PurchaseLog> result = new Result<>();

        try {
            if (purchaseLog.getCurrent() == null || purchaseLog.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<PurchaseLog> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            wrapper.orderByDesc("create_time");
            IPage<PurchaseLog> purchaseLogs = purchaseLogMapper.selectPage(new Page<>(purchaseLog.getCurrent(), purchaseLog.getSize()), wrapper);
            result.setReturnPage(purchaseLogs);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<PurchaseLog> queryPurchaseLogById(PurchaseLog purchaseLog) {
        Result<PurchaseLog> result = new Result<>();
        try {
            PurchaseLog returnPurchaseLog = purchaseLogMapper.selectById(purchaseLog.getPurchaseLogId());
            result.setReturnObj(returnPurchaseLog);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 录入采购，修改采购状态
     * @param purchaseLog
     * @return
     */
    @Override
    public Result<PurchaseLog> updateStatus(PurchaseLog purchaseLog) {
        Result<PurchaseLog> result = new Result<>();
        try {
            //采购状态修改为采购状态
            purchaseLog.setPurchaseStatus("1");
            purchaseLogMapper.updateById(purchaseLog);
            //根据采购的材料ID设置该材料总数
            Material material = materialMapper.selectById(purchaseLog.getMaterialId());
            material.setAmount(material.getAmount()+purchaseLog.getAmount());
            materialMapper.updateById(material);
            //新建库存日志
            MaterialLog materialLog = new MaterialLog();
            materialLog.setMaterialLogId(IDGenerator.getUUID("MALOG"));
            materialLog.setMaterialId(purchaseLog.getMaterialId());
            materialLog.setMaterialName(purchaseLog.getMaterialName());
            materialLog.setAmount(purchaseLog.getAmount());
            materialLog.setUnit(purchaseLog.getUnit());
            //设置为入库状态
            materialLog.setLogType("1");
            materialLog.setCreateUser(purchaseLog.getCreateUser());
            materialLog.setCreateUserName(purchaseLog.getCreateUserName());
            materialLog.setCreateTime(new Date());

            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
